const request = require("../../../utils/request.js").request;
const router = require("../../../utils/router.js").router;
const timeUtil = require("../../../utils/timeUtil.js").timeUtil;
Page({

    data: {
        invoices: [],
    },

    onShow: function () {
        var that = this;
        const userModal = wx.getStorageSync('user');
        request(router.getInvoices, "GET", {
            userModal: userModal,
        }).then(function(res) {
            let resData = res.data;
            if (resData.code == 502) {
                wx.navigateTo({
                  url: '../../login/login',
                })
                return;
            }
            if (resData.code == 200) {
                const invoices = resData.data.invoices;
                for (var i = 0; i < invoices.length; i++) {
                    var date = timeUtil.DateStrToDate(invoices[i].date);
                    invoices[i].date = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
                    var applyDate = timeUtil.DateStrToDate(invoices[i].applyDate);
                    applyDate = timeUtil.toLocalDate(applyDate);
                    invoices[i].applyDate = timeUtil.formatDateToStr(applyDate);
                }
                that.setData({
                    invoices: invoices,
                })
            }
        })
    },

    toDetail: function(e) {
        let index = e.currentTarget.dataset.index;
        if (index > this.data.invoices.length - 1 || index < 0) {
            return;
        }
        let invoice = this.data.invoices[index];
        let invoiceStr = JSON.stringify(invoice);
        wx.navigateTo({
          url: './myInvoiceDetail/myInvoiceDetail?invoice=' + invoiceStr,
        })
    },

})